package com.auto.learning.sharesdk.tools;

import android.content.Context;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat.ShareParams;

import com.auto.learning.R;
import com.auto.learning.bean.ShareModelBean;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.FileUtil;
import com.tencent.mm.sdk.modelbase.BaseResp.ErrCode;

public class ShareOpUtils {

	public static void doShareQQ(Context context, Callback callback, PlatformActionListener listener,String title,String url,String content,String imageUrl) {
		QZone.ShareParams sp = new QZone.ShareParams();
		sp.setTitle(title);
		sp.setTitleUrl(url); // 标题的超链接
		sp.setText(content);
		if (TextUtils.isEmpty(imageUrl)) {
			sp.setImagePath(FileUtil.getFileCachePath()+"/icon.png");
		}else {
			sp.setImageUrl(imageUrl);
		}
		sp.setSite(context.getString(R.string.app_name));
		sp.setSiteUrl(url);
		Platform qzone = ShareSDK.getPlatform(context, QZone.NAME);
		qzone.setPlatformActionListener(listener);
		qzone.share(sp);
	}
	
	public static void doShareSina(Context context, Callback callback, PlatformActionListener listener,String title,String url,String content,String imageUrl){
		OnekeyShare oks = new OnekeyShare();
		oks.setNotification(R.drawable.icon,context.getString(R.string.app_name));
		if (content != null) {
			content = content+" "+url;
			oks.setText(content);
		} else {
			oks.setText(context.getString(R.string.app_name));
		}
		oks.setTitle(title);
		oks.setUrl(url);
		if (TextUtils.isEmpty(imageUrl)) {
			oks.setImagePath(FileUtil.getFileCachePath()+"/icon.png");
		}else {
			oks.setImageUrl(imageUrl);
		}
		oks.setSilent(false);
		oks.setTheme(OnekeyShareTheme.SKYBLUE);
		oks.setPlatform(SinaWeibo.NAME);
		oks.setDialogMode();
		oks.show(context);
		
		
		/*ShareParams sp = new ShareParams();
		sp.setText(content +" " +url);
		if (TextUtils.isEmpty(imageUrl)) {
			sp.setImagePath(FileUtil.getFileCachePath()+"/icon.png");
		}else {
			sp.setImageUrl(imageUrl);
		}
		Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
		weibo.setPlatformActionListener(listener); // 设置分享事件回调
		weibo.share(sp);*/
	}
	
	public static void doShareWeixin(Context context, Callback callback,String weixinType, PlatformActionListener listener,String title,String url,String content,String imageUrl,int type){
		Platform plat = null;
		ShareParams sp = new ShareParams();
		sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setUrl(url);
		if (weixinType.equals(ShareCommInfo.WEIXIN_CHAT)) {
			sp.setTitle(title);
			sp.setText(content);
		} else if(weixinType.equals(ShareCommInfo.WEIXIN_FRIEND)){
			if (type == 0) {
				sp.setTitle("《" + title + "》" + " " +content);
			}else if (type == 1){
				sp.setTitle(title + " | " +content);
			}else {
				sp.setTitle(title + "\n" +content);
			}
		}
		
		if (TextUtils.isEmpty(imageUrl)) {
			sp.setImagePath(FileUtil.getFileCachePath()+"/icon.png");
		} else {
			sp.setImageUrl(imageUrl);
		}
		plat = ShareSDK.getPlatform(weixinType);
		plat.setPlatformActionListener(listener);
		plat.share(sp);
	}
	
	// 处理分享回调
	public static void handleShareCallBack(Context context, Message msg) {
		switch (msg.what) {
			case ShareCommInfo.SHARE_SUCCESS:
				CommonMethod.makeNotice(context, "分享成功");
				ShareModelBean shareModelBean = (ShareModelBean) msg.obj;
				HttpRequestHelper.addUserShare(context, shareModelBean.getShareType(), shareModelBean.getShareTag(), shareModelBean.getShareId());
				break;
			case ShareCommInfo.SHARE_CANCLE:
				CommonMethod.makeNotice(context, "取消分享");
				break;
			case ShareCommInfo.SHARE_FAIL_NO_CLENT:
				CommonMethod.makeNotice(context, "请先安装微信客户端");
				break;
			case ShareCommInfo.SHARE_FAIL_SEND_ERROR:
				CommonMethod.makeNotice(context, "请检查网络连接");
				break;
		}
	}
	
}
